<template>
  <a-drawer
    v-model:visible="vdata.visible"
    title="门店入驻"
    width="70%"
    @close="onClose"
  >
    <a-divider orientation="left" style="color: #1A66FF;">商户号：{{ vdata.mchNo }}</a-divider>
    <JeepayTable
      ref="infoTable"
      :initData="false"
      :reqTableDataFunc="reqTableDataFunc"
      :tableColumns="tableColumns"
      :searchData="vdata.searchData"
      rowKey="storeId"
      @btnLoadClose="vdata.btnLoading=false"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'state'">
          <a-badge v-if="record.state == 0" status="default" text="未入驻" />
          <a-badge v-if="record.state == 1" status="warning" text="审核中" />
          <a-badge v-if="record.state == 2" status="success" text="已入住" />
          <a-badge v-if="record.state == 3" status="error" text="入驻失败" />
          <a-tooltip v-if="record.state == 3" :title="record.msg">
            <info-circle-outlined />
          </a-tooltip>
        </template>
        <template v-if="column.key === 'op'">
          <!-- 操作列插槽 -->
          <JeepayTableColumns>
            <a-button type="link" :disabled="vdata.btnLoading" @click="applyment(record.storeId, record.state)">入驻门店</a-button>
          </JeepayTableColumns>
        </template>
      </template>
    </JeepayTable>
  </a-drawer>
</template>

<script lang="ts" setup>
import { API_URL_MCH_STORE_LIST, API_URL_MCH_APPLYMENT_LIST, req, $applymentStore } from '@/api/manage'
import { ref, reactive, getCurrentInstance } from 'vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

const tableColumns = reactive([
  { title: '门店编号', dataIndex: 'storeId', fixed: 'left', width: '100px' },
  { title: '门店名称', dataIndex: 'storeName', width: '120' },
  { title: '入驻状态', key: 'state', dataIndex: 'state', width: '80' },
  { title: '入驻编号', dataIndex: 'storeNo', width: '80' },
  { key: 'op',title: '操作', fixed: 'right', align: 'center', width: '120' }
])
const props = defineProps({
  callbackFunc: {
    type: Function,
    default: () => ({})
  }
})

const infoTable = ref()

const vdata : any = reactive({
  recordId: null, // 更新对象ID
  visible: false, // 是否显示弹层/抽屉
  tableColumns: tableColumns,
  btnLoading: false
})

function show (recordId, mchNo) { // 弹层打开事件
  vdata.recordId = recordId
  req.getById(API_URL_MCH_APPLYMENT_LIST, recordId).then(res => {
    vdata.storeParams = JSON.parse(res.storeSuccResParameter)
  })

  vdata.mchNo = mchNo
  vdata.visible = true
  reqTableDataFunc(null)
}
// 请求table接口数据
function reqTableDataFunc (params)  {
  return req.list(API_URL_MCH_STORE_LIST, {mchNo: vdata.mchNo}).then((res) => {
    res.records.forEach(item => {
          item.state = 0
          item.storeNo = ''
          item.msg = ''
          vdata.storeParams.forEach(element => {
            if(item.storeId == element.storeId) {
              item.state = element.state
              item.storeNo = element.storeNo
              item.msg = element.msg
            }
          })
      })
    return Promise.resolve(res)
  })
}

function onClose () {
  vdata.visible = false
}

function applyment (storeId, state) {
  vdata.btnLoading = true
  $infoBox.confirmDanger('确认入驻该门店吗？', '', () => {
    $applymentStore(storeId, state, vdata.recordId, vdata).then(res => {
        vdata.btnLoading = false
        $infoBox.message.success('入驻成功')
        infoTable.value.refTable(true)
        props.callbackFunc() // 刷新列表
        vdata.loading = false
      }).finally(() => {
    })
    vdata.btnLoading = false
  })
}


defineExpose({ show })
</script>
<style scoped>

.form-item-content{
  width: 70%
}

</style>